# Methods added to this helper will be available to all templates in the application.
require 'net/http'

module ApplicationHelper
  
  def sanitize_all html
    sanitize(html, :tags => []).gsub(/&nbsp\;/,'') if html.present?
  end 
  
  def redirect_to(url)
    "window.location.href='#{url}'"
  end
  
  def to_taobao_uri(string)
    URI.escape Iconv.iconv("GBK//IGNORE","UTF-8//IGNORE",string).to_s
  end
  
  def remote_will_paginate(models,html_opinions = {})
    html_opinions = {
      :go_to_js_function =>'go_to'
    }.merge( html_opinions)
    page_range_count = 3
    # 翻页范围
    #start_page = models.current_page - page_range_count if (models.current_page > page_range_count)
    #start_page = 1 if (models.current_page <= page_range_count)
    
    # «上一页 1...6,7,8...100 下一页»    
    if((models.current_page) > page_range_count) and ((models.current_page + page_range_count) < models.total_pages)
      logger.debug "1...xcx...t"
      link_to_function( '«上一页' , "#{html_opinions[:go_to_js_function]}(#{models.previous_page})", :class =>'page_num') +  
      link_to_function( 1 , "#{html_opinions[:go_to_js_function]}(1)", :class =>'page_num' ) + 
      ((models.current_page - page_range_count) == 1 ? '' : "<span class='blank'>...</span>")  +
      ((models.current_page - page_range_count + 1) .. (models.current_page + page_range_count )).to_a.each_with_object([]) do |page_num, array|
        array << link_to_function( page_num , 
                  "#{html_opinions[:go_to_js_function]}(#{page_num})", 
                  :class => "page_num #{page_num == models.current_page ? 'current_page' : ''}" ) 
      end.join('') +
      "<span class='blank'>...</span>" +
      link_to_function( models.total_pages , "#{html_opinions[:go_to_js_function]}(#{models.total_pages})", :class =>'page_num' ) + 
      link_to_function( '下一页»' , "#{html_opinions[:go_to_js_function]}(#{models.next_page})", :class =>'page_num')  
    # «上一页 1,2...6,7,8 下一页» 
    elsif(models.current_page <= page_range_count) and ((models.current_page + page_range_count) < models.total_pages)
      logger.debug "1,c,3...(t-1),t"
      link_to_function( '«上一页' , "#{html_opinions[:go_to_js_function]}(#{models.previous_page})", :class =>'page_num') +  
      (1.. page_range_count).to_a.each_with_object([]) do |page_num, array|
        array << link_to_function( page_num , 
                  "#{html_opinions[:go_to_js_function]}(#{page_num})", 
                  :class => "page_num #{page_num == models.current_page ? 'current_page' : ''}" ) 
      end.join('') +
      link_to_function( (page_range_count + 1) , 
                  "#{html_opinions[:go_to_js_function]}(#{page_range_count + 1})", 
                  :class => "page_num #{(page_range_count + 1) == models.current_page ? 'current_page' : ''}" ) + 
      "<span class='blank'>...</span>" +
      ((models.total_pages - page_range_count) .. models.total_pages).to_a.each_with_object([]) do |page_num, array|
        array << link_to_function( page_num , 
                  "#{html_opinions[:go_to_js_function]}(#{page_num})", 
                  :class => "page_num #{page_num == models.current_page ? 'current_page' : ''}" ) 
      end.join('') +
      link_to_function( '下一页»' , "#{html_opinions[:go_to_js_function]}(#{models.next_page})", :class =>'page_num')
    # «上一页 1...6,7,8 下一页»  
    elsif ((models.current_page) > page_range_count) and ((models.current_page + page_range_count) >= models.total_pages)
      logger.debug "1...xcxt"
      link_to_function( '«上一页' , "#{html_opinions[:go_to_js_function]}(#{models.previous_page})", :class =>'page_num') +  
      link_to_function( 1 , "#{html_opinions[:go_to_js_function]}(1)", :class =>"page_num #{1 == models.current_page ? 'current_page' : ''}" ) +      
      "<span class='blank'>...</span>" +
      ((models.current_page - page_range_count ) .. models.total_pages ).to_a.each_with_object([]) do |page_num, array|
        array << link_to_function( page_num , 
                  "#{html_opinions[:go_to_js_function]}(#{page_num})", 
                  :class => "page_num #{page_num == models.current_page ? 'current_page' : ''}" ) 
      end.join('') +
      link_to_function( '下一页»' , "#{html_opinions[:go_to_js_function]}(#{models.next_page})", :class =>'page_num') 
    # «上一页 1,2,3,4,5,6 下一页»  
    else
      logger.debug "123ct"
      link_to_function( '«上一页' , "#{html_opinions[:go_to_js_function]}(#{models.current_page == 1 ? 1 : models.previous_page})", :class =>'page_num') +
      (1 .. models.total_pages ).to_a.each_with_object([]) do |page_num, array|
        array << link_to_function( page_num , 
                  "#{html_opinions[:go_to_js_function]}(#{page_num})", 
                  :class => "page_num #{page_num == models.current_page ? 'current_page' : ''}" ) 
      end.join('') +  
      link_to_function( '下一页»' , "#{html_opinions[:go_to_js_function]}(#{models.current_page == models.total_pages ? models.total_pages : models.next_page})", :class =>'page_num')
    end
  end
end
